package com.liutaw.nanjingair.presenter.products;

import com.liutaw.nanjingair.views.activities.products.IRadarView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liutao on 16/3/17.
 */
public class RadarPresenter implements IRadarPresenter {
    //test
    public static final String[] images = {"http://img2.imgtn.bdimg.com/it/u=3093785514,1341050958&fm=21&gp=0.jpg",
            "http://img2.3lian.com/2014/f2/37/d/40.jpg",
            "http://d.3987.com/sqmy_131219/001.jpg",
            "http://img2.3lian.com/2014/f2/37/d/39.jpg",
            "http://www.8kmm.com/UploadFiles/2012/8/201208140920132659.jpg",
            "http://f.hiphotos.baidu.com/image/h%3D200/sign=1478eb74d5a20cf45990f9df460b4b0c/d058ccbf6c81800a5422e5fdb43533fa838b4779.jpg",
            "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg"
    };
    private String startTimePrefix;
    private String endTimePrefix;
    private Date endDate;
    private Date startDate;

    private IRadarView radarView;

    public RadarPresenter(IRadarView radarView) {
        this.radarView = radarView;
    }

    @Override
    public void requestPicUrlLists() {
        //模拟载入数据
        List<String> urlStrings = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            urlStrings.add(images[i]);
        }
        radarView.onPicsLoaded(urlStrings);

        try {
            initTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isValide(String hour, String minute, boolean isStartTime) {
        if (isStartTime) {
            //开始时间
            String startTime = startTimePrefix + hour + ":" + minute;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                Date date = format.parse(startTime);
                if (date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime()) {
                    radarView.onStartTimeTextLoaded(startTime);
                    return true;
                }
                return false;
            } catch (ParseException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            String endTime = endTimePrefix + hour + ":" + minute;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                Date date = format.parse(endTime);
                if (date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime()) {
                    radarView.onStartTimeTextLoaded(endTime);
                    return true;
                }
                return false;
            } catch (ParseException e) {
                e.printStackTrace();
                return false;
            }
        }
    }

    private void initTime() throws ParseException {
        long current = System.currentTimeMillis();
        long twoHourBefore = current - 2 * 60 * 60 * 1000;
        endDate = new Date();
        endDate.setTime(current);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat prefixFormat = new SimpleDateFormat("yyyy-MM-dd ");
        startDate = new Date();
        startDate.setTime(twoHourBefore);
        format.format(startDate);
        String startTime = format.format(startDate);
        String endTime = format.format(endDate);
        radarView.onStartTimeTextLoaded(startTime);
        radarView.onEndTimeTextLoaded(endTime);
        startDate = format.parse(startTime);
        endDate = format.parse(endTime);
//        radarView.onTimeDateInit(startDate, endDate);
        startTimePrefix = prefixFormat.format(endDate);
        endTimePrefix = prefixFormat.format(startDate);
    }
}
